let editingId = null;
let requestUrl = null;

/**
 * 法宝穿戴状态
 */
let stateArray = ["未穿戴", "穿戴中"];
/**
 * 法宝两仪类型
 */
let liangyiArray = [null, "阴", "阳"];

function addHandler() {
	requestUrl = "/fabao/insert";

	$("#fabao_dialog").dialog("open");
}

function editHandler() {
	let rowData = $("#fabao_list").datagrid("getSelected");
	
	if (rowData) {
		requestUrl = "/fabao/updateById";

		$("#id").val(rowData.id);
		$("#roleId").combobox("setValue", rowData.roleId);
		$("#grade").numberspinner("setValue", rowData.grade);
		$("#categoryId").combobox("setValue", rowData.categoryId);
		$("#wearStatus").combobox("setValue", rowData.wearStatus);

		$("#fabao_dialog").dialog("open");
	} else {
		alertMsg("请选择要修改的记录！", "warning");
	}
}

function improveHandler() {
	let rowData = $("#fabao_list").datagrid("getSelected");

	if (rowData) {
		let fabaoId = rowData.id;

		$("#liangyi_list").datagrid("reload", {
			fabaoId: fabaoId
		});

		$("#sixiang_effect").empty().hide();
		
		ajaxGet("/fabao_sixiang_attribute/selectByFabaoId", {
			fabaoId: fabaoId
		}, function (resp) {
			let data = resp.data;

			if (data) {
				$("#sixiang_effect").html(data.name + "：" + data.value).show();
			}
		}, error);

		$("#improve_dialog").dialog("open");
	} else {
		alertMsg("请选择要修改的记录！", "warning");
	}
}

function deleteHandler() {
	let rowData = $("#fabao_list").datagrid("getSelected");

	if (rowData) {
		$.messager.confirm("提示", "是否确认删除？", function(bool) {
			if (bool) {
				ajaxPost("/fabao/deleteById", {
					id: rowData.id
				}, function(resp) {
					showMsg(resp.message);

					$("#fabao_list").datagrid("reload");
				}, error);
			}
		});
	} else {
		alertMsg("请选择要删除的法宝！");
	}
}

function computeHandler() {
	let rowData = $("#fabao_list").datagrid("getSelected");

	if (rowData) {
		ajaxPost("/fabao/compute", {
			fabaoId: rowData.id
		}, function(resp) {
			showMsg(resp.message);

			$("#fabao_list").datagrid("reload");
		}, error);
	} else {
		unselected();
	}
}

$(document).ready(function() {

	$("#role_id").combobox({
		url: "/role_account/selectAll",
		valueField: "id",
		textField: "name",
		width: 150,
		panelHeight: "auto",
		prompt: "-选择角色-",
		formatter: function(row) {
			ajaxGet("/role_category/selectById", {
				id: row.categoryId
			}, function(resp) {
				let data = resp.data;

				$("#modeling_" + row.id).attr("src", data.image);
			}, error);

			return "<img height='18' id='modeling_" + row.id + "' />&nbsp;" + row.name;
		}
	});

	$("#category_id").combobox({
		url: "/fabao_category/selectAll",
		valueField: "id",
		textField: "name",
		width: 150,
		panelHeight: "auto",
		prompt: "--选择法宝类型--",
		formatter: function(row) {
			return "<img height='18' src='" + row.image + "' />&nbsp;" + row.name;
		}
	});

	$("#search").linkbutton({
		iconCls: "icon-search"
	}).click(function() {
		let categoryId = $("#category_id").combobox("getValue");
		let roleId = $("#role_id").combobox("getValue");

		$("#fabao_list").datagrid("reload", {
			categoryId: categoryId,
			roleId: roleId
		});
	});

	$("#clear").linkbutton({
		iconCls: "icon-delete"
	}).click(function() {
		$("#search_form").form("clear");
	});

	/******************************************************************************************************/

	$("#categoryId").combobox({
		url: "/fabao_category/selectAll",
		valueField: "id",
		textField: "name",
		width: 120,
		required: true,
		panelHeight: "auto",
		formatter: function(row) {
			return "<img src='" + row.image + "' width='18' />&nbsp;" + row.name;
		}
	});

	$("#grade").numberspinner({
		min: 1,
		max: 10,
		width: 120,
		required: true
	});

	/**
	 * 穿戴状态
	 */
	fetchSelect("#wearStatus", stateArray, {
		width: 120,
		required: true
	});

	$("#roleId").combobox({
		url: "/role_account/selectAll",
		valueField: "id",
		textField: "name",
		width: 120,
		required: true,
		panelHeight: "auto",
		formatter: function(row) {
			ajaxGet("/role_category/selectById", {
				id: row.categoryId
			}, function(resp) {
				let data = resp.data;

				$("#role_category_" + row.id).attr("src", data.image);
			}, error);

			return "<img width='18' id='role_category_" + row.id + "' />&nbsp;" + row.name;
		}
	});

	$("#fabao_dialog").dialog({
		title: "法宝信息",
		height: 300,
		width: 500,
		buttons: [{
			iconCls: "icon-save",
			text: "保存",
			handler: function() {
				let selector = "#fabao_form";

				checkForm(selector, function () {
					let data = $(selector).serialize();

					ajaxPost(requestUrl, data, function(resp) {
						showMsg(resp.message);

						$(selector).form("clear");
						$("#fabao_dialog").dialog("close");
						$("#fabao_list").datagrid("reload");
					}, error);
				});
			}
		}, {
			iconCls: "icon-cancel",
			text: "取消",
			handler: function() {
				$("#fabao_dialog").dialog("close");
				$("#fabao_form").form("clear");
			}
		}]
	});

	/******************************************************************************************************/

	$("#liangyi_list").datagrid({
		url: "/fabao_liangyi_attribute/selectByFabaoId",
		title: "法宝两仪属性",
		method: "get",
		height: 200,
		width: 520,
		onAfterEdit: function (rowIndex, rowData, changes) { // 结束行内编辑事件
			data = {
				id: rowData.id,
				fabaoId: rowData.fabaoId,
				step: changes.step ? changes.step : rowData.step,
				valueId: changes.valueId ? changes.valueId : rowData.valueId,
				liangyiId: changes.liangyiId ? changes.liangyiId : rowData.liangyiId
			};
		},
		toolbar: [{
			iconCls: "icon-add",
			text: "添加",
			handler: function() {
				let rowData = $("#fabao_list").datagrid("getSelected");

				if(rowData) {
					let datagrid = $("#liangyi_list");

					datagrid.datagrid("appendRow",{
						id: null,
						step: 1,
						valueId: null,
						liangyiId: null,
						fabaoId: rowData.id
					});

					let total = datagrid.datagrid("getRows").length;

					editingId = total - 1;
					requestUrl = "/fabao_liangyi_attribute/insert";

					datagrid.datagrid("beginEdit", editingId);

					let $editor_ = datagrid.datagrid("getEditor", {
						index: editingId,
						field: "liangyiId"
					});

					$($editor_.target).combobox("reload", {
						fabaoId: rowData.categoryId
					});
				} else {
					unselected();
				}
			}
		}, "-", {
			iconCls: "icon-edit",
			text: "修改",
			handler: function() {
				let datagrid = $("#liangyi_list");

				if (editingId != null) {
					datagrid.datagrid("selectRow", editingId);
				} else {
					let rowData = datagrid.datagrid("getSelected");

					if (rowData) {
						// 获取行索引，这个索引从0开始
						let rowIndex = datagrid.datagrid("getRowIndex", rowData);

						editingId = rowIndex;
						requestUrl = "/fabao_liangyi_attribute/updateById";

						datagrid.datagrid("beginEdit", rowIndex);

						let $editor_ = datagrid.datagrid("getEditor", {
							index: rowIndex,
							field: "liangyiId"
						});

						let fabao = $("#fabao_list").datagrid("getSelected");

						$($editor_.target).combobox("reload", {
							fabaoId: fabao.categoryId
						});
					} else {
						unselected();
					}
				}
			}
		}, "-", {
			iconCls: "icon-save",
			text: "保存",
			handler: function() {
				if (editingId != null) {
					// 只有结束编辑才能获取到最新的值
					$("#liangyi_list").datagrid("endEdit", editingId);

					ajaxPost(requestUrl, data, function(resp) {
						editingId = null;

						showMsg(resp.message);

						$("#liangyi_list").datagrid("reload");

						let rowData = $("#fabao_list").datagrid("getSelected");

						if (rowData) {
							ajaxGet("/fabao_sixiang_attribute/selectByFabaoId", {
								fabaoId: rowData.id
							}, function (resp) {
								let data = resp.data;

								if (data) {
									$("#sixiang_effect").html(data.name + "：" + data.value).show();
								}
							}, error);
						}
					}, error);
				}
			}
		}, "-", {
			iconCls: "icon-cancel",
			text: "取消",
			handler: function() {
				if (editingId != null) {
					$("#liangyi_list").datagrid("cancelEdit", editingId);

					editingId = null;
				}
			}
		}, "-", {
			iconCls: "icon-delete",
			text: "删除",
			handler: function() {
				let rowData = $("#liangyi_list").datagrid("getSelected");

				if (rowData) {
					$.messager.confirm("提示", "是否确认删除？", function(bool) {
						if (bool) {
							ajaxPost("/fabao_liangyi_attribute/deleteById", {
								id: rowData.id
							}, function(resp) {
								showMsg(resp.message);

								$("#liangyi_list").datagrid("reload");
							}, error);
						}
					});
				} else {
					alertMsg("请选择要删除的记录！");
				}
			}
		}],
		columns: [[
			{field: "id", hidden: true},
			{field: "fabaoId", hidden: true},
			{field: "step", title: "进阶次数", align: "center", width: 100
				, editor: {
					type: "numberspinner",
					options: {
						min: 1,
						max: 3
					}
				}
			},
			{field: "liangyiId", title: "两仪属性", align: "center", width: 250
				, editor: {
					type: "combobox",
					options: {
						url: "/fabao_liangyi_attribute_category/selectByFabaoId",
						valueField: "id",
						textField: "note",
						panelHeight: "auto",
						formatter: function (record) {
							return liangyiArray[record.type] + "：" + record.note;
						}
					}
				}
				, formatter: function(value, rowData, rowIndex) {
					if (value) {
						ajaxGet("/fabao_liangyi_attribute_value/selectById", {
							id: rowData.valueId
						}, function (resp) {
							let attributeValue = resp.data;

							ajaxGet("/fabao_liangyi_attribute_category/selectById", {
								id: value
							}, function (resp) {
								let data = resp.data;
								let content = (attributeValue.propertyValue * data.max) + ((data.isPercent === 1) ? "%" : "");

								$("#liangyi_" + rowIndex).html(data.effect.replaceAll("x", content));
							}, error);
						}, error);

						return "<div id='liangyi_" + rowIndex + "' />";
					}
				}
			},
			{field: "valueId", title: "属性数值", align: "center", width: 160
				, editor: {
					type: "combobox",
					options: {
						url: "/fabao_liangyi_attribute_value/selectAll",
						valueField: "id",
						textField: "text",
						panelHeight: "auto"
					}
				}
				, formatter: function(value, rowData, rowIndex) {
					if (value) {
						ajaxGet("/fabao_liangyi_attribute_value/selectById", {
							id: value
						}, function (resp) {
							let data = resp.data;

							$("#value_" + rowIndex).html(data.text);
						}, error);

						return "<div id='value_" + rowIndex + "' />";
					}
				}
			}
		]]
	});

	$("#improve_dialog").dialog({
		title: "法宝进阶",
		height: 260,
		modal: true,
		closed: true,
		closable: true,
		draggable: false
	});

	/******************************************************************************************************/

	/**
	 * 表格体的高度
	 * @type {number}
	 */
	let heigt_body = heigt_tr_with_role_details * 10;
	/**
	 * 表格高度
	 * @type {number}
	 */
	let height_table = heigt_tool + heigt_header + heigt_body + heigt_footer + heigt_scoll_bar + 2;

	$("#fabao_list").datagrid({
		url: "/fabao/selectByPage",
		method: "get",
		height: height_table,
		fitColumns: true,
		rownumbers: true,
		pagination: true,
		toolbar: [{
			iconCls: "icon-add",
			text: "添加",
			handler: function() {
				addHandler();
			}
		}, "-", {
			iconCls: "icon-edit",
			text: "修改",
			handler: function() {
				editHandler();
			}
		}, "-", {
			iconCls: "icon-shield",
			text: "进阶",
			handler: function() {
				improveHandler();
			}
		}, "-", {
			iconCls: "icon-delete",
			text: "删除",
			handler: function() {
				deleteHandler();
			}
		}, "-", {
			iconCls: "icon-ok",
			text: "计算评分",
			handler: function() {
				computeHandler();
			}
		}],
		columns: [[
			{field: "categoryId", title: "法宝类型", align: "center", width: 160
				, formatter: function(value, rowData, rowIndex) {
					ajaxGet("/fabao_category/selectById", {
						id: value
					}, function(resp) {
						let data = resp.data;

						$("#fabao_category_" + rowIndex).prop({
							title: data.name,
							src: data.image,
							alt: data.name
						});
					}, error);

					return "<img height='" + size + "' id='fabao_category_" + rowIndex + "' />";
				}
			},
			{field: "grade", title: "法宝等级", align: "center", width: 160},
			{field: "score", title: "法宝评分", align: "center", width: 160},
			{field: "wearStatus", title: "穿戴状态", align: "center", width: 160
				, formatter: function(value) {
					return "<span>" + stateArray[value] + "</span>";
				}
			},
			roleDetailsColumn
		]]
	});

});